import 'https://cdn.bootcdn.net/ajax/libs/jquery/2.2.0/jquery.js';

class Cartlist {
    constructor() {

    }

    init() {
        let localObj = {};
        if (localStorage.getItem('goods')) {
            localObj = JSON.parse(localStorage.getItem('goods'))
        }
        // console.log(Object.keys(localObj));


        $.ajax({
            url: 'http://localhost/www.gome.com.cn/zkl/interface/cartlist.php',
            dataType: 'json'
        }).then(function(allData) {
            // console.log(allData);

            $.each(Object.keys(localObj), function(index, localValue) {
                console.log(localValue.substring(8));
                $.each(allData, function(index, dataValue) {
                    // console.log(dataValue.sid);
                    if (localValue.substring(8) === dataValue.sid) {
                        let clonebox = $('.cart-list .goods-item:hidden').clone(true, true);
                        // console.log(clonebox);
                        clonebox.attr('sid', dataValue.sid);
                        clonebox.css('display', 'block');
                        clonebox.find('.td-item img').attr('src', dataValue.url)
                        clonebox.find('.item-title span').html(dataValue.title)
                        clonebox.find('.td-price span').html(dataValue.price)
                        clonebox.find('.amunt-box input').val(localObj[localValue])
                        clonebox.find('.td-sum strong').html(dataValue.price * localObj[localValue])


                        $('.cart-wrapper .cart-list').append(clonebox);
                        calc();
                    }
                })
            })
        })

        function calc() {
            let sum = 0;
            let price = 0;
            $('.cart-list .goods-item:visible').each(function(index, element) {
                // console.log(element);
                if ($(element).find('.td-chk input').prop('checked')) {
                    sum += parseFloat($(element).find('.amunt-box input').val());
                    price += parseFloat($(element).find('.td-sum strong').html());
                }
            });
            console.log(sum);
            console.log(price);
            $('.carttoolbar .right .collect em').html(sum);
            $('.carttoolbar .price-sum .p1 i').html(price);
        }

        // 复选框操作
        $('.allsel').on('click', function() {
            // 4.prop()读写元素对象属性(不包括自定义的属性)
            // 3.attr(name/pro/key,val/fn)读写元素对象属性(包括自定义的属性)
            $('.cart-list .goods-item:visible').find('.td-chk input').prop('checked', $(this).prop('checked'));
            $('.allsel').prop('checked', $(this).prop('checked'));
            calc();
        })


        $('.cart-list').on('click', '.td-chk input', function() {
            // alert('1')
            let len1 = $('.cart-list .td-chk input').length;
            let len2 = $('.cart-list .td-chk input:checked').length;
            if (len1 === len2) {
                $('.allsel').prop('checked', true);
            } else {
                $('.allsel').prop('checked', false);
            }
            calc()
        })


        //数量改变操作
        $('.cart-list').on('click', '.amunt-add', function() {
            // alert('1')
            let num = $(this).prev().val();
            num++;
            $(this).prev().val(num);
            $(this).parents('.goods-item').find('.td-sum strong').html(single($(this)));
            calc();
            storage($(this).parents('.goods-item').attr('sid'), num);
            // console.log(single($(this)));
        })

        $('.cart-list').on('click', '.amunt-down', function() {
            let num = $(this).next().val();
            num--;
            if (num < 1) {
                num = 1;
            }
            $(this).next().val(num);
            $(this).parents('.goods-item').find('.td-sum strong').html(single($(this)));
            calc();
            storage($(this).parents('.goods-item').attr('sid'), num);
        })

        $('.cart-list').on('input', '.amunt-box input', function() {
            // alert('1')
            let reg = /^[1-9]\d*$/;
            if (!reg.test($(this).val())) {
                $(this).val(1)
            }
            $(this).parents('.goods-item').find('.td-sum strong').html(single($(this)));
            calc();
            storage($(this).parents('.goods-item').attr('sid'), parseInt($(this).val()));
        })


        //封装函数计算小计
        function single(obj) {
            let count = parseInt(obj.parents('.goods-item').find('.td-amunt .amunt-box input').val());
            // let count= parseInt(obj.parents('.goods-item').find('.cart-list .td-amunt input').val());
            let price = parseInt(obj.parents('.goods-item').find('.td-price span').html());
            return (count * price).toFixed(2);
        }


        //封装函数改变本地localstrog
        function storage(sid, num) {
            localObj['product_' + sid] = num;
            localStorage.setItem('goods', JSON.stringify(localObj))
                // localStorage.setItem(localObj[ ])
        }
    }


}

export {
    Cartlist
}